/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class HandleDataTableCampaignHourlyV2 {
public static final String KEYSPACE                             =   "AdsMobileDataResultKs";

public static final String ColumFamlyBannerClickCountCf         =   "MobileBannerClickCountCf";
public static final String ColumFamlyBannerViewCountCf          =   "MobileBannerViewCountCf";

public static final String ColumFamlyCampaignClickCountCf       =   "MobileCampaignClickCountCf";
public static final String ColumFamlyCampaignViewCountCf        =   "MobileCampaignViewCountCf";

public void handleData() throws ParseException, InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
    //handle data by v3_cam_date
    long first_time = System.currentTimeMillis();
    CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
    cassAct.initReadWirte(KEYSPACE);
    
    MysqlDataManager mysql = new MysqlDataManager();
    MysqlDataManagerMain mysqlMain = new MysqlDataManagerMain();
    MysqlDataManager67 mysql67 = new MysqlDataManager67();
        
    List listcamps = mysqlMain.getCampIDByMobile();       
    String currentDate = HandleDataTableDomainDate.handleDate();//2012-07-24
    //String currentDate = "2013-08-12";
    CheckPauseBannerAndCampaign cbc = new CheckPauseBannerAndCampaign();        
    //get data pause
    HashSet dataPause = new HashSet();
    dataPause = cbc.getDataStatusPause(mysql67, currentDate);
    
    //String currentDate = "2013-03-15";    
    Debug.println("ngay :"+currentDate);
    String[] arrStr = new String[3];
    arrStr = currentDate.split("-");
    String changeDay = arrStr[2]+"/"+arrStr[1]+"/"+arrStr[0];
    StringBuilder sbff = new StringBuilder();    
    Iterator it=listcamps.iterator();
    
    while(it.hasNext())
    {
        String key = (String)it.next();   
        
        //if campaignid pause
        if(dataPause.contains(key))
        {
            continue;
        }
                     
        if(key.length() > 1)
        {                       
            String str = getClickCampaignHour(cassAct, changeDay, changeDay, key, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf);
            sbff.append(str);              
        } else {
            Debug.println(key);
            Debug.println("fail");
            long last_time = System.currentTimeMillis();
            System.out.println("Thoi gian chay  :" +(last_time-first_time));
            continue;
        }              
    }

    //xu li cho du lieu cuoi
    if(sbff.toString().length() > 1)
    {
        //MysqlDataManager mysql = new MysqlDataManager();
        //xu li cho du lieu cuoi
        //connect to DB 6.7
        //MysqlDataManager67 mysql67 = new MysqlDataManager67();
        String relStr = sbff.toString();
        String valInsert = relStr.substring(0, (relStr.length()-1));        
        HandleImportData hid = new HandleImportData();
        String pathFile="";        
        try {
            pathFile = hid.outToCsvCampaignHourly(valInsert);
            Debug.println("pathFile:"+pathFile);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        try {            
            //get sys_date_cpc                                                  
            mysql.removeDataCampaignHourlyByDate(currentDate);        
            mysql.loadToV3CamHourlyFromFile(pathFile);
                                  
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    //close connecion ot DB
    mysql.shutdownConnection();  
    mysqlMain.shutdownConnection();
    
    long last_time = System.currentTimeMillis();
    System.out.println("Thoi gian chay  :" +(last_time-first_time));
    Debug.println("success HandleDataTableCampaignHourly");
}

public static String getClickCampaignHour(CassandraAction cassAct,String startDay,String finishDay,String key,String columnFamilyClick,String columnFamilyView) throws ParseException{

        StringBuilder sb = new StringBuilder();      
        String dayStart = startDay.split("/")[0];
        String dayLast = finishDay.split("/")[0];
        String prefix = "-" +startDay.split("/")[1]+"-"+startDay.split("/")[2];

        String prefixDB = startDay.split("/")[2] + "-" + startDay.split("/")[1] + "-";

        for(int i = Integer.valueOf(dayStart) ; i <= Integer.valueOf(dayLast) ;i++)
        {
            for(int hour =0 ;hour <=23 ;hour ++)
            {
                if(hour < 10)
                {
                    if(hour > 5){
                        
                        String date = "0"+ hour +":00:00 " + String.valueOf(i) + prefix ;
                        long start_time = TimeUtil.longToDateWithHour(date);
                        //them 1h
                        long finish_time = start_time + 60*60*1000 - 1;
                        long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                        long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);

                        if(countClick > 0)
                        {
                            String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                            sb.append(str).append(",");
                        }
                    } else {
                        String date = "0"+ hour +":00:00 " + String.valueOf(i) + prefix;//cong them 1 ngay nua
                        long start_time = TimeUtil.longToDateWithHour(date) + 24*60*60*1000;
                        //them 1h
                        long finish_time = start_time + 60*60*1000 - 1;
                        long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                        long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);

                        if(countClick > 0)
                        {
                            String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                            sb.append(str).append(",");
                        }
                        
                    }
                   
                } else {                       
                    String date = hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h 
                    long finish_time = start_time + 60*60*1000 - 1;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);

                    if(countClick > 0)
                    {
                        String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                        sb.append(str).append(",");
                    }                   
                }
            }
        }
        return sb.toString();
    }

}
